package cn.easy.service.impl;

import cn.easy.api.UserService;
import cn.easy.common.pojo.EasyResult;
import cn.easy.common.util.UserUUID;
import cn.easy.mapper.UserMapper;
import cn.easy.pojo.User;
import cn.easy.pojo.UserExample;
import com.alibaba.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * @author 谢子豪
 * @time 2017/10/3
 * @description 用户操作业务层
 */
@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;

    /**
     * 新增一个用户
     *
     * @param user
     * @return
     */
    @Override
    public void insertUser(User user) throws Exception {
        //判断用户的登录名是否已经重复
        UserExample userExample = new UserExample();
        UserExample.Criteria criteria = userExample.createCriteria();
        criteria.andUserLoginNameEqualTo(user.getUserLoginName());
        List<User> users = userMapper.selectByExample(userExample);
        if (users.size() != 0) {
            throw new Exception("账户已经存在");
        }
        //获取当前系统时间
        Date date = new Date();
        user.setUserCreateTime(date);
        user.setUserUpdateTime(date);
        //获取随机的key
        String userUUID = UserUUID.getUserUUID();
        user.setUserKey(userUUID);
        //设置用户状态，在注册情况下用户的状态的都是可用
        user.setUserState(1);
        //将用户信息插入数据库
        userMapper.insert(user);
    }

    /**
     * 根据用户账号查询用户基本信息
     *
     * @param loginName
     * @return 返回用户信息, 如果
     */
    @Override
    public User searchUserByLoginName(String loginName) throws Exception {
        User user = userMapper.selectByLoginName(loginName);
        return user;
    }

    /**
     * 根据用户昵称查询用户基本信息
     *
     * @param nickName
     * @return
     */
    @Override
    public List<User> searchUserByNickName(String nickName) throws Exception {
        List<User> list = userMapper.selectByNickName(nickName);
        return list;
    }

    /**
     * 更新用户信息，需要验证用户权限,注意这里不能更新用户的key,也不能更新用户的状态
     *
     * @param user
     * @return
     */
    @Override
    public void updateUser(User user) throws Exception {
        //验证用户身份
        User oldUser = userMapper.selectByPrimaryKey(user.getUserId());
        if (oldUser == null) {
            throw new Exception("用户不存在");
        }
        if (!oldUser.getUserKey().equals(user.getUserKey())) {
            throw new Exception("用户身份验证失败，请检查key");
        }
        //设置用户更新时间
        oldUser.setUserUpdateTime(new Date());
        //更新用户
        if (user.getUserNickName() != null) oldUser.setUserNickName(user.getUserNickName());
        if (user.getUserPassword() != null) oldUser.setUserPassword(user.getUserPassword());
        if (user.getUserPhone() != null) oldUser.setUserPhone(user.getUserPhone());
        if (user.getUserEmail() != null) oldUser.setUserEmail(user.getUserEmail());
        userMapper.updateByPrimaryKey(oldUser);
    }

    /**
     * 更新用户的key
     *
     * @param userKey
     * @ return
     */
    @Override
    public String updateUserKey(String userKey) throws Exception {
        UserExample example = new UserExample();
        UserExample.Criteria criteria = example.createCriteria();
        criteria.andUserKeyEqualTo(userKey);
        List<User> users = userMapper.selectByExample(example);
        if (users.size() > 0) {
            User user = users.get(0);
            user.setUserKey(UserUUID.getUserUUID());
            userMapper.updateByPrimaryKey(user);
            //获取新的用户key
            User newUser = userMapper.selectByPrimaryKey(user.getUserId());
            return newUser.getUserKey();
        } else {
            throw new Exception("用户身份验证失败，请检查key");
        }
    }

    /**
     * 通过账号密码 获取用户的key
     *
     * @param userLoginName
     * @param userPassword
     * @return
     */
    @Override
    public String getUserKey(String userLoginName, String userPassword) throws Exception {
        UserExample example = new UserExample();
        UserExample.Criteria criteria = example.createCriteria();
        criteria.andUserLoginNameEqualTo(userLoginName);
        criteria.andUserPasswordEqualTo(userPassword);
        List<User> users = userMapper.selectByExample(example);
        if (users.size() == 0) throw new Exception("身份验证失败，请检查账号密码");
        User user = users.get(0);
        return user.getUserKey();
    }

    /**
     * 用户登录
     *
     * @param userLoginName 用户名
     * @param userPassword  密码
     * @return
     */
    @Override
    public User login(String userLoginName, String userPassword) {
        User user = userMapper.login(userLoginName, userPassword);
        return user;
    }

    @Override
    public User getUserInfoByUserId(String userKey, Integer userId) throws Exception {
        User user = userMapper.selectByPrimaryKey(userId);
        if(user == null){
            throw new Exception("找不到用户");
        }
        if(!user.getUserKey().equals(userKey)){
            throw  new Exception("身份验证失败");
        }
        return user;
    }

    @Override
    public void Ceshi() {

    }
}
